<?php
namespace app\admin\model;

use other\ExportExcel;
use other\FanJianConvert;
use think\Db;
use think\Request;
use think\Session;
use think\Validate;

//公用功能类库

class UserModel extends CommonModel
{
/*
用户管理
 */






    /**
     * 左侧菜单列表显示
     * @author 星  2018/10/11
     */
    public function menuListss()
    {
        $roleid = db("bs_roleid")->where('id',Session::get("user")['roleid'])->find();
        if($roleid['grade']==1){													//判断是否是管理员
            $menu = db("bs_menu")->alias("p")
                ->join('la_public_lang l','(p.menu_id = l.data_id and l.lang_type = "bs_menu/menu_name")','LEFT')
                ->field("p.*,l.hk_name")
                ->where('p.menu_hidden','1')->where("p.is_delete",'1')->order('p.menu_order asc')->select();		//查询所有的菜单数据
        } else {
            $role = db("bs_roleid")->where('id',Session::get("user")['roleid'])->find();		//用户角色数据
            $fids = db("bs_admin_power")->where('roleid_id',$role['id'])->column("function_id");//相应角色的功能id
            $mids = db("bs_function")->where('id','in',$fids)->where('is_delete','1')->column("func_menuid");//相应的菜单id
            $menu = db("bs_menu")->alias("p")
                    ->join('la_public_lang l','(p.menu_id = l.data_id and l.lang_type = "bs_menu/menu_name")','LEFT')
                ->field("p.*,l.hk_name")
                ->where('p.menu_hidden','1')->where('p.menu_id','in',$mids)->order('p.menu_order asc')->select();//有操作权限的菜单
        }
        if($menu) {
            $tet = array();
            foreach ($menu as $key => $value) {
                $menu_id = $this->getChildrenId($value['menu_id']);
                $tet = array_merge($tet,$menu_id);
            }

            $data1 = db("bs_menu")->where("menu_id","in",$tet)->where('is_delete','1')->where('menu_hidden','1')->select();

            $data =array();
            //遍历获取所有的上级菜单
            foreach ($menu as $v) {
                if ($v['parentid']!=0) {
                    $data[] = $this->getMenuId($v['parentid']);
                }
                $data[] = array($v);
            }
            $array = array();
            //重组菜单数据类型
            foreach ($data as $key => $value) {
                foreach ($value as $k => $v) {
                    $array[] = $v;
                }
            }
            $array = array_merge($data1,$array);
            $data = array_unique(array_column($array,"menu_id"));//剔除相同的menu_id
            $menus = db("bs_menu")->alias("p")
                ->join('la_public_lang l','(p.menu_id = l.data_id and l.lang_type = "bs_menu/menu_name")','LEFT')
                ->field("p.*,l.hk_name")
                ->where('p.menu_id','in',$data)
                ->order('p.menu_order asc')
                ->select();//根据menu_id查询相应的菜单数据
            $menu = $this->getTree($menus,0);                     //调取递归组装分类树
        }
        return $menu;
    }

    /**
     * 组装分类树
     * @param array $data 查到的菜单数据
     * @param int 	$pId  父级id
     * @author 星  2018/10/11
     */
    public function getTree($data, $pId)
    {
        $tree = array();													//声明空字符串
        foreach($data as $k => $v) {
            if($v['parentid'] == $pId) {
                $v['son'] = $this->getTree($data, $v['menu_id']);   //递归
                $tree[] = $v;
            }
        }
        return $tree;
    }


    /**
     * 递归查询子类的id
     * @param [int] $menu_id  逻辑删除数据的menu_id
     * @author 星  2018/9/12
     */
    public function getChildrenId($menu_id)
    {
        static $ret = array();      //静态声明数组
        $data = db("bs_menu")->where('parentid',$menu_id)->select();    //获取下一级菜单
        if(count($data)>0){
            foreach ($data as $key => $v) {
                if ($v['parentid']==$menu_id) {
                    $ret[] = $v['menu_id'];
                    $this->getChildrenId($v['menu_id']);    //获取所有的菜单id
                }
            }
        }
        return $ret;
    }

    /**
     * 递归查询顶级父id
     * @param   int 	$id 	父级id
     * @param   array 	$data   递归后所有的上级菜单数据
     * @author  星  2018/10/11
     */
    public function getMenuId($id,$data=array()){
        $rec = Db::name('bs_menu')->where('menu_id',$id)->find();//根据父级id查询父级
        $data[] = $rec;											 //赋值给data
        if ($rec['parentid'] != '0') {
            return $this->getMenuId($rec['parentid'],$data);	 //递归循环
        }
        return $data;
    }

    /**
     * 查询菜单id
     * @param   int 	$id 	父级id
     * @param   array 	$data   递归后所有的上级菜单数据
     * @author  星  2018/10/11
     */
    public function getMenuIds($ids){
        $menuIds = db("bs_function")->where("id","in",$ids)->column("func_menuid");
        $menuIds = array_unique($menuIds);
        $parentid = db("bs_menu")->where('menu_id','in',$menuIds)->column("parentid");
        $parentid = array_unique($parentid);
        $menu_id = array();
        foreach ($parentid as $key=>$id) {
            if($id!=0) {
                $menu = db("bs_menu")->where("menu_id",$id)->find();
                if($menu['parentid']!=0) {
                    $menu_id[] = db("bs_menu")->where('menu_id',$menu['parentid'])->value("menu_id");
                }
                $menu_id[] = $menu['menu_id'];
            }
        }
        $funcId = db("bs_function")->where('func_menuid','in',$menu_id)->column("id");
        $funcIds = array_merge($ids,$funcId);

        return $funcIds;
    }

}